package com.qfedu.edu.manager;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * @author xiaobobo
 * @title: RedisManager
 * @projectName seckill-fy-2401-parent
 * @description: TODO
 * @date 2024/11/4  15:38
 */
@Component
public class RedisManager {


    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 获取String类型的数据
     * @param token
     * @return
     */
    public String getString(String token) {
        return  stringRedisTemplate.opsForValue().get(token);
    }

    /**
     * 向String类型中添加数据
     */
    public void addString(String key, String val, Integer expire, TimeUnit timeUnit) {
        stringRedisTemplate.opsForValue().set(key,val,expire,timeUnit);
    }


    /**
     * 添加键值对的信息到Redis中
     * @param key
     * @param val
     * @param expire
     * @param timeUnit
     */
    public void addObject(String key, Object val, Integer expire, TimeUnit timeUnit){
        redisTemplate.opsForValue().set(key,val,expire,timeUnit);
    }


    /**
     * 通过key获取值
     * @param key
     * @return
     */
    public Object getObject(String key){
        return redisTemplate.opsForValue().get(key);
    }

    /**
     * 从String中删除数据
     * @param token
     */
    public void deleteString(String token) {
        stringRedisTemplate.delete(token);
    }

    /**
     * 计数
     * @param key
     * @param step
     */
    public void incr(String key, int step) {
        redisTemplate.opsForValue().increment(key,step);
    }
}
